#include "ad_serchgood.h"
#include "ui_ad_serchgood.h"
#include "guoshu.h"
#include <QStandardItem>
#include <QDateTime>
#include <QMessageBox>
#include "value.h"

ad_serchgood::ad_serchgood(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ad_serchgood)
{
    ui->setupUi(this);
    setWindowTitle("查询商品");
}

ad_serchgood::~ad_serchgood()
{
    delete ui;
}

void ad_serchgood::on_serchbtn_clicked()//查询产品
{
    tmodel=new QStandardItemModel(js(),5,this);
    QStringList headlist;
    headlist<<"编号"<<"品名"<<"产品形式"<<"进货时间"<<"剩余数量";
    tmodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(tmodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    struct good *tmp;
    tmp=p;
    int cnt=0;
    std::string goodname=ui->goodnum->text().trimmed().toStdString();
    QStandardItem *aItem;
    if(ui->comboBox->currentIndex()==0){
        for(int i=0;i<count1;i++){
            if(strcmp(goodname.c_str(),tmp->name)==0){
                //编号
                aItem=new QStandardItem(QString::asprintf("%s",tmp->number));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,0,aItem);
                //品名
                aItem=new QStandardItem(QString::asprintf("%s",tmp->name));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,1,aItem);
                //形式
                std::string selltyp;
                selltyp="盒装";
                if(tmp->typ==1) selltyp="散装";
                aItem=new QStandardItem(QString::asprintf("%s",selltyp.c_str()));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,2,aItem);
                //进货时间
                aItem=new QStandardItem(QString::asprintf("%s",tmp->purchasedate));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,3,aItem);
                //剩余数量
                aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,4,aItem);
                cnt++;
            }
            tmp=tmp->next;
        }
        if(cnt==0){
            QMessageBox::warning(this,"err","未找到商品");
            return;
        }
        else{
            char gname[50];
            strcpy(gname,goodname.c_str());
            seach0(gname,cnt);
        }
    }
    else{
        for(int i=0;i<count1;i++){
            if(strcmp(goodname.c_str(),tmp->number)==0){
                //编号
                aItem=new QStandardItem(QString::asprintf("%s",tmp->number));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,0,aItem);
                //品名
                aItem=new QStandardItem(QString::asprintf("%s",tmp->name));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,1,aItem);
                //形式
                std::string selltyp;
                selltyp="盒装";
                if(tmp->typ==1) selltyp="散装";
                aItem=new QStandardItem(QString::asprintf("%s",selltyp.c_str()));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,2,aItem);
                //进货时间
                aItem=new QStandardItem(QString::asprintf("%s",tmp->purchasedate));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,3,aItem);
                //剩余数量
                aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
                aItem->setTextAlignment(Qt::AlignHCenter);
                tmodel->setItem(cnt,4,aItem);
                cnt++;
            }
            tmp=tmp->next;
        }
        if(cnt==0){
            QMessageBox::warning(this,"err","未找到商品");
            return;
        }
        else {
            char gname[50];
            strcpy(gname,goodname.c_str());
            seach1(gname,cnt);
        }
    }
}

int ad_serchgood::js()//搜索此产品数量
{
    struct good *tmp;
    tmp=p;
    int cnt=0;
    std::string goodname=ui->goodnum->text().trimmed().toStdString();
    if(ui->comboBox->currentIndex()==0){
        for(int i=0;i<count1;i++){
            if(strcmp(goodname.c_str(),tmp->name)==0){
                cnt++;
            }
            tmp=tmp->next;
        }
    }
    else{
        for(int i=0;i<count1;i++){
            if(strcmp(goodname.c_str(),tmp->number)==0){
                cnt++;
            }
            tmp=tmp->next;
        }
    }
    return cnt;
}

void ad_serchgood::seach1(char *s_1, int nn)
{
    QStandardItem *aItem;
    QDateTime curtime=QDateTime::currentDateTime();
    double count02=0;  //剩余数量
    struct good *tmp;
    tmp=p;
    for(int i=0;i<count1;i++){
        if(p==NULL) break;
        if(strcmp(s_1,tmp->number)==0){
            count02+=tmp->count;    //剩余数量
        }
        tmp=tmp->next;
    }
    struct good *tmp1;
        tmp1=ss2;
        double count03=0;   //售出数量
        double outprice3=0;   //售出金额
        double profit=0;      //利润
        for(int i=0;i<count2;i++){
            if(ss2==NULL) break;
            if(strcmp(s_1,tmp1->number)==0){
                count03+=tmp1->count;   //售出数量
                outprice3+=tmp1->outprice * tmp1->count;    //售出金额
                profit+=(tmp1->outprice - tmp1->inprice) * tmp1->count; //利润
            }
            tmp1=tmp1->next;
        }
        aItem=new QStandardItem(QString::asprintf("剩余数量"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,0,aItem);
        aItem=new QStandardItem(QString::asprintf("售出数量"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,1,aItem);
        aItem=new QStandardItem(QString::asprintf("售出金额"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,2,aItem);
        aItem=new QStandardItem(QString::asprintf("利润"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,3,aItem);
        //printf("剩余数量：%.2lf\n",count02);
        //printf("售出数量：%.2lf\n",count03);
        //printf("售出金额：%.2lf\n",outprice3);
        //printf("利润：%.2lf\n",profit);
        nn++;
        aItem=new QStandardItem(QString::asprintf("%.2lf",count02));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,0,aItem);
        aItem=new QStandardItem(QString::asprintf("%.2lf",count03));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,1,aItem);
        aItem=new QStandardItem(QString::asprintf("%.2lf",outprice3));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,2,aItem);
        aItem=new QStandardItem(QString::asprintf("%.2lf",profit));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,3,aItem);
}

//求生产日期至当前日期的天数
int day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end)
{
    int y2, m2, d2;
    int y1, m1, d1;
    m1 = (month_start + 9) % 12;
    y1 = year_start - m1 / 10;
    d1 = 365 * y1 + y1 / 4 - y1 / 100 + y1 / 400 + (m1 * 306 + 5) / 10 + (day_start - 1);
    m2 = (month_end + 9) % 12;
    y2 = year_end - m2 / 10;
    d2 = 365 * y2 + y2 / 4 - y2 / 100 + y2 / 400 + (m2 * 306 + 5) / 10 + (day_end - 1);
    return (d2 - d1);
}

void ad_serchgood::seach0(char *s_1,int nn)//品名查找
{
    QStandardItem *aItem;
    QDateTime curtime=QDateTime::currentDateTime();
    double count02=0;  //剩余数量
    struct good *tmp;
    tmp=p;
    for(int i=0;i<count1;i++){
        if(p==NULL) break;
        if(strcmp(s_1,tmp->name)==0){
            count02+=tmp->count;    //剩余数量
        }
        tmp=tmp->next;
    }
    struct good *tmp1;
        tmp1=ss2;

        double count03=0;   //售出数量
        double outprice3=0;   //售出金额
        double profit=0;      //利润
        for(int i=0;i<count2;i++){
            if(ss2==NULL) break;
            if(strcmp(s_1,tmp1->name)==0){
                count03+=tmp1->count;   //售出数量
                outprice3+=tmp1->outprice * tmp1->count;    //售出金额
                profit+=(tmp1->outprice - tmp1->inprice) * tmp1->count; //利润
            }
            tmp1=tmp1->next;
        }
        aItem=new QStandardItem(QString::asprintf("剩余数量"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,0,aItem);
        aItem=new QStandardItem(QString::asprintf("售出数量"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,1,aItem);
        aItem=new QStandardItem(QString::asprintf("售出金额"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,2,aItem);
        aItem=new QStandardItem(QString::asprintf("利润"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,3,aItem);
        //printf("剩余数量：%.2lf\n",count02);
        //printf("售出数量：%.2lf\n",count03);
        //printf("售出金额：%.2lf\n",outprice3);
        //printf("利润：%.2lf\n",profit);
        nn++;
        aItem=new QStandardItem(QString::asprintf("剩余数量：%.2lf",count02));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,0,aItem);
        aItem=new QStandardItem(QString::asprintf("售出数量：%.2lf",count03));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,1,aItem);
        aItem=new QStandardItem(QString::asprintf("售出金额：%.2lf",outprice3));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,2,aItem);
        aItem=new QStandardItem(QString::asprintf("利润：%.2lf",profit));
        aItem->setTextAlignment(Qt::AlignHCenter);
        tmodel->setItem(nn,3,aItem);
}
